#include <string.h>

#include "datasource.h"
#include "config.h"

DataSource::DataSource(){
	current_node=new char[50];
	index=0;
	updown=0; //up
	//file_name="C:\\skype.avt";
	//file_name=config.scheduler_.trace_file.c_str();
	finput.open(config.scheduler_.trace_file.c_str(),ifstream::in);
}
DataSource::~DataSource(){
	delete[] current_node;
}

struct DataItem* DataSource::NextItem(){
	//skip lines of comments starting with '#'
	while(finput.peek()==35)
        finput.ignore(5000,'\n');
	//read data
	while(true){
        if(index==0){//begin reading sessions of next node
           finput>>current_node>>session_number;
		   if(session_number==0) continue;
		   index++;
           finput>>time;
		   struct DataItem* di = &data_item;
		   strcpy(di->node_id,current_node);
		   di->state=(status)updown;
		   updown=(updown+1)%2;
		   di->time=time;
		   return di;
		}
	    else{
            if(index>session_number){
				index=0;
				updown=0;  //up
				continue;
			}
			finput>>time;
			struct DataItem* di= &data_item;
			strcpy(di->node_id,current_node);
			di->state=(status)updown;
			updown=(updown+1)%2;
			di->time=time;
            if(updown==0) index++;
			return di;
		}
	}
}

